package com.zy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @author LittleDesire
 * @description
 * @date 2022-04-06 周三 21:45
 * @since 17
 */
@Controller
public class FetchController {
    @PostMapping("fetch") @ResponseBody
    public String upload1(MultipartFile file, String user, HttpServletRequest req) {
        String msg = "上传失败";
        if (file.isEmpty()) {
            msg = "上传失败";
        } else {
            try {
                long fileSize = file.getSize();
                String fileName = file.getOriginalFilename();
                assert fileName != null;
                // lastIndexOf 指定子字符串最后一次出现的索引，如果没有出现，则返回-1 。
                // extension name 后缀名
                String fileExt = fileName.lastIndexOf(".") == -1 ? "" : fileName.toLowerCase().substring(fileName.lastIndexOf("."));
                // %tY%<tm%<td%<tH%<tM%<tS 年与日时分秒
                String filePath = req.getServletContext().getRealPath(String.format("uploads/%tY%<tm%<td",System.currentTimeMillis()));
                File f = new File(filePath);
                if (f.exists()) {
                    f.mkdirs();
                }
                msg = "上传成功："+ fileName;
                file.transferTo(new File(filePath,fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.format("%s: %s", user, msg);
    }


    @PostMapping("/fetchs")
    @ResponseBody
    public String uploads(MultipartFile[] file, String user, HttpServletRequest req) {
        String msg = "上传失败，请选择一个文件";

        if (file.length == 0) {
            msg = "上传失败，请选择一个文件";
        } else {
            Set<String> set = new HashSet<String>();
            for (MultipartFile fs : file) {
                if(!fs.isEmpty()) {
                    try {
                        String filename = fs.getOriginalFilename();
                        String fileext = filename.lastIndexOf(".") == -1 ? "" : filename.toLowerCase().substring(filename.lastIndexOf("."));
                        long filesize = fs.getSize();
                        String path = req.getServletContext().getRealPath(String.format("uploads/%tY%<tm%<td", System.currentTimeMillis()));
                        File dst = new File(path);
                        if (!dst.exists()) dst.mkdirs();

                        set.add(filename);
                        fs.transferTo(new File(path, filename));

                    } catch (IOException e) {
                        msg = "上传失败，未知错误";
                    }
                }
            }
            if(set.isEmpty())  msg = "上传成功，文件：'" + set + "'";
        }
        return String.format("%s:%s", user, msg);
    }
}
